package com.zp.kidshop.common.util;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.LinkedList;

import org.apache.log4j.Logger;

import com.zp.kidshop.common.pojo.SysProp;


/**
 * 
 * @Title: LogUtil.java
 * @Package com.minghang.common.utils 
 * @Description: 日志记录工具类
 * @author 张鹏  
 * @date 2017年2月16日 下午3:15:41 
 * @version V1.0
 */
public class LogUtil {
	private static ThreadLocal<LinkedList<String>> logList = new ThreadLocal<LinkedList<String>>();
	private static Logger logger = Logger.getLogger(LogUtil.class);
	private static final String SIGN = "----------------------------------------------------------------------";
	private static final int SIGN_LEN = SIGN.length();
	
	public static final String INFO = "INFO";
	public static final String WARN = "WARN";
	public static final String FATAL = "FATAL";
	public static final String ERROR = "ERROR";
	
	private LogUtil(){}
	/**
	 *	LinkedList放入本地线程变量
	 *@param lList	LinkedList<String>
	 *@return void
	 **/
	public static void setLogList(LinkedList<String> lList){
		logList.set(lList);
	}
	
	/**
	 *	返回当前线程LinkedList
	 *@return LinkedList<String>
	 **/
	public static LinkedList<String> getLogList(){
		return logList.get();
	}
	
	/**
	 *	返回当前线程LinkedList
	 *@return LinkedList<String>
	 **/
	public static void remove(){
		logList.remove();
	}
	
	/**
	 *	根据List组织日志头
	 *@return void
	 **/
	private static void buildHead(){
		LinkedList<String> lList = logList.get();
		String title = lList.removeFirst();
		String subSign = null;
		if (SIGN_LEN > title.getBytes().length) {
			subSign = SIGN.substring(0, (SIGN_LEN - title.getBytes().length)/2);
			lList.addFirst(subSign.concat(title).concat(subSign));
		}else {
			lList.addFirst(title);
		}
		lList.addFirst(SysProp.getThisTime());
		lList.addFirst(SIGN);
		lList.addLast(SIGN+"\n");
	}

	/**
	 *	添加一条日志到List
	 *@param logContent	日志信息
	 *@return void
	 **/
	public static void addLog(String logContent){
		logList.get().add(logContent);
	}

	/**
	 *	根据List组织日志信息
	 **/
	public static String buildLogBody(){
		if (null == logList.get() || logList.get().isEmpty()) {
			return null;
		}
		StringBuilder sBuilder = new StringBuilder();
		buildHead();
		Iterator<String> iterator = logList.get().iterator();
		sBuilder.append("\r\n");
		while (iterator.hasNext()) {
			sBuilder.append(iterator.next()).append("\r\n");
		}
		remove();
		return sBuilder.toString();
	}

	/**
	 *根据logType写入相应日志文件
	 *@param logType	：输出模式LogUtil.INFO 等
	 *@param throwable	：异常e
	 *@return void
	 **/
	public static void print(String logType,Throwable throwable){
		String content = null;
		if (null == (content = buildLogBody())) {
			return;
		}

		switch (logType) {
		case WARN:
			logger.warn(content);
			if (null != throwable)
				logger.warn(throwable.getMessage(),throwable);
			break;
		case ERROR:
			logger.error(content);
			if (null != throwable)
				logger.error(throwable.getMessage(),throwable);
			break;
		case FATAL:
			logger.fatal(content);
			if (null != throwable)
				logger.fatal(throwable.getMessage(),throwable);
			break;
		case INFO:
			logger.info(content);
			if (null != throwable)
				logger.info(throwable.getMessage(),throwable);
			break;
		}
	}
	
	/**
	 *	根据logType写入相应日志文件
	 *@param logType	:输出模式LogUtil.INFO 等
	 *@param logContent	:需要输出的信息
	 *@return void
	 **/
	public static void printC(String logType,String logContent){
		switch (logType) {
		case WARN:
			logger.warn(logContent);
			break;
		case ERROR:
			logger.error(logContent);
			break;
		case FATAL:
			logger.fatal(logContent);
			break;
		case INFO:
			logger.info(logContent);
			break;
		}
	}
	
	/**
	 * 提取错误信息
	 **/
	public static String getExceptionInfo(Throwable e) {
		String errInfo = null;
		try(ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			PrintStream printStream = new PrintStream(byteArrayOutputStream);){
			e.printStackTrace(printStream);
			errInfo = new String(byteArrayOutputStream.toByteArray());
			printStream.flush();
			byteArrayOutputStream.flush();
		} catch (Exception e1) {
			errInfo = "[ExceptionHandler 取出异常信息出错]:".concat(e1.getMessage());
		}
		return errInfo;
	}

	/**
	 * 直接输出严重错误信息
	 **/
	public static void pringFatal(String message,Throwable e){
		if (null == e) {
			logger.fatal(message);
		}else {
			logger.fatal(message, e);
		}
	}
}
